import dayjs from "dayjs";
import { getSysTime } from "@/api/user";

export function convertUnderscoreToUpperCase(str: string) {
  return str.replace(/_([a-z])/g, function (match, group1) {
    return group1.toUpperCase();
  });
}

/* 去除对象中的空置 */
export function clearEmptyValues(obj: object): object {
  const newObj = {};
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      const value = obj[key];
      if (value === null || value === undefined || value === "") {
        // delete obj[key];
      } else {
        newObj[key] = value;
      }
    }
  }
  return newObj;
}

/* 对象数组去重 */
export function deWeightArr(arr = [], keyName = "id") {
  return arr.reduce((accumulator, current) => {
    const existingItem = accumulator.find(
      item => item[keyName] === current[keyName]
    );
    if (!existingItem) {
      accumulator.push(current);
    }
    return accumulator;
  }, []);
}

/* 计算两个时间差 */
var timeFlag = false;
export function diffDate(startTime = null, endTime = null, format?, callback?) {
  format = format ?? 1;
  let nowDate: any = new Date();
  if (!timeFlag) {
    timeFlag = true;
    getSysTime().then(res => {
      nowDate = res?.data || 0;
      setTimeout(() => {
        timeFlag = false;
      }, 1000 * 30);
    });
  }

  startTime = dayjs(startTime || nowDate);
  endTime = dayjs(endTime || nowDate);
  let duration = Math.abs(endTime.diff(startTime, "minute"));

  /* format : 1输出格式xx小时xx分钟 2 xx天xx小时xx分钟 */
  let h = Math.floor(duration / 60);
  let m = duration % 60;

  if (format == 2) {
    duration = Math.abs(endTime.diff(startTime));
    const days = duration / 1000 / 60 / 60 / 24;
    const hours = (duration / 1000 / 60 / 60) % 24;
    const minutes = (duration / 1000 / 60) % 60;
    callback && callback({ days, hours, minutes });

    return `${Math.floor(days)}天${Math.floor(hours)}小时${Math.floor(minutes)}分钟`;
  }
  callback && callback({ h, m });
  return `${h}时${m}分钟`;
}

export const getNowTime = () => getSysTime();

// export const colorArr = [
//   "#1F78FF",
//   "#EE6D3C",
//   "#1366F5",
//   "#0AEB8B",

//   "#0426E1",
//   "#895BF1",
//   "#00D3FB",
//   "#F0F04C",
//   "#F0B74C",
//   "#E05156",
//   "#EF478C"
// ];

export const colorArr = [
  "#5470c6",
  "#91cc75",
  "#fac858",
  "#ee6666",
  "#73c0de",
  "#3ba272",
  "#fc8452",
  "#9a60b4",
  "#ea7ccc"
];

/* 计算设计图实际宽高 */
export const realWH = num => {
  return {
    w: Number(parseFloat((num / 1920) * window.innerWidth).toFixed(2)),
    h: Number(parseFloat((num / 1080) * window.innerHeight).toFixed(2))
  };
};
